home *** CD-ROM | disk | FTP | other *** search
/ Super PC 34 / Super PC 34 (Shareware).iso / spc / UTIL / DJGPP2 / V2 / DJLSR200.ZIP / src / libc / posix / utime / utime.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-02-26  |  1.5 KB  |  53 lines

  1. /* Copyright (C) 1995 DJ Delorie, see COPYING.DJ for details */
  2. #include <libc/stubs.h>
  3. #include <utime.h>        /* For utime() */
  4. #include <time.h>        /* For localtime() */
  5. #include <fcntl.h>        /* For open() */
  6. #include <unistd.h>
  7. #include <errno.h>        /* For errno */
  8. #include <go32.h>
  9. #include <dpmi.h>
  10.  
  11. /* An implementation of utime() for DJGPP.  The utime() function
  12.    specifies an access time and a modification time.  DOS has only one
  13.    time, so we will (arbitrarily) use the modification time. */
  14. int
  15. utime(const char *path, const struct utimbuf *times)
  16. {
  17.   __dpmi_regs r;
  18.   struct tm *tm;
  19.   time_t modtime;
  20.   int fildes;
  21.   unsigned int dostime, dosdate;
  22.  
  23.   /* DOS wants the file open */
  24.   fildes = open(path, O_RDONLY);
  25.   if (fildes == -1) return -1;
  26.  
  27.   /* NULL times means use current time */
  28.   if (times == NULL)
  29.     modtime = time((time_t *) 0);
  30.   else
  31.     modtime = times->modtime;
  32.  
  33.   /* Convert UNIX time to DOS date and time */
  34.   tm = localtime(&modtime);
  35.   dosdate = tm->tm_mday + ((tm->tm_mon + 1) << 5) +
  36.     ((tm->tm_year - 80) << 9);
  37.   dostime = tm->tm_sec / 2 + (tm->tm_min << 5) +
  38.     (tm->tm_hour << 11);
  39.  
  40.   /* Set the file timestamp */
  41.   r.h.ah = 0x57; /* DOS FileTimes call */
  42.   r.h.al = 0x01; /* Set date/time request */
  43.   r.x.bx = fildes; /* File handle */
  44.   r.x.cx = dostime; /* New time */
  45.   r.x.dx = dosdate; /* New date */
  46.   __dpmi_int(0x21, &r);
  47.  
  48.   /* Close the file */
  49.   (void) close(fildes);
  50.  
  51.   return 0;
  52. }
  53.